package base.juc;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * CallableDemo 案列
 */
public class CallableDemo {
    /**
     * 实现 runnable 接口
     */
    static class MyThread1 implements Runnable{
        /**
         * run 方法
         */
        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName()
                        + "线程进入了 run 方法");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    /**
     * 实现 callable 接口
     */
    static class MyThread2 implements Callable{
        /**
         * call 方法
         * @return
         * @throws Exception
         */
        @Override
        public Long call() throws Exception {
            try {
                System.out.println(Thread.currentThread().getName()
                                + "线程进入了 call 方法,开始准备睡觉");
                        Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + "睡醒了");
            }catch (Exception e){
                e.printStackTrace();
            }
            return System.currentTimeMillis();
        }
    }
    public static void main(String[] args) throws Exception{
        //声明 runable
        Runnable runable = new MyThread1();
        //声明 callable
        Callable callable = new MyThread2();
        //future-callable
        FutureTask<Long> futureTask2 = new FutureTask(callable);
        //线程二
        new Thread(futureTask2, "线程二").start();
        for (int i = 0; i < 10; i++) {
            Long result1 = futureTask2.get();
            System.out.println(result1);
        }
        //线程一
        new Thread(runable,"线程一").start();
    }
}
